using System;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Threading;
using System.Text;

namespace Bootstrap_Project.Web.Controllers
{
    public class ContestController : Controller
    {
        static string xml = null;
        //
        // GET: /Contest/
        public ViewResult Index()
        {
            return View();
        }

        [System.Web.Http.HttpPost]
        public void SetXml([FromBody] string xmlString)
        {
            ContestController.xml = xmlString;
        }

        public FileResult DownloadExcel()
        {
            while (ContestController.xml == null)
                Thread.Sleep(1000);
            string str = Server.UrlDecode(ContestController.xml);
            byte[] bytes = new byte[str.Length * sizeof(char)];
            bytes = Encoding.UTF8.GetBytes(str.ToCharArray());
            ContestController.xml = null;
            return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Fixtures.xls");
        }

        public ActionResult Template(string id)
        {
            switch (id)
            {
                case "ownerTab":
                    return PartialView("~/Views/Contest/_ownerChartenerTab.cshtml");
                case "portsTab":
                    return PartialView("~/Views/Contest/_portsTab.cshtml");
                case "ordersTab":
                    return PartialView("~/Views/Contest/_ordersTab.cshtml");
                case "OCForm":
                    return PartialView("~/Views/Contest/OCForm.cshtml");
                case "portsManageForm":
                    return PartialView("~/Views/Contest/_portsManageForm.cshtml");
                case "portsZones":
                    return PartialView("~/Views/Contest/_portsZones.cshtml");
                case "layCan":
                    return PartialView("~/Views/Contest/_layCan.cshtml");
                case "CP":
                    return PartialView("~/Views/Contest/_CPDate.cshtml");
                case "portsPopup":
                    return PartialView("~/Views/Contest/_portsPopup.cshtml");
                case "dateTimePopup":
                    return PartialView("~/Views/Contest/_dateTimePopup.cshtml");
                default:
                    throw new Exception("template not known");
            }
        }
    }
}